<?php
/**
 * Report
 *-------------------------
 *
 * Deal with menu display, selected status. Top menu - sub menu 
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from Smarter Ver4.0, which is a comprehensive shopping engine
 * that helps consumers to make smarter buying decisions online. We empower consumers to compare
 * the attributes of over one million products in the computer and consumer electronics categories
 * and to read user product reviews in order to make informed purchase decisions. Consumers can then
 * research the latest promotional and pricing information on products listed at a wide selection of
 * online merchants, and read user reviews on those merchants.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2005, Mezimedia. All rights reserved.
 *
 * @author     Steven.Ying <steven_ying@mezimedia.com>
 * @copyright  (C) 2004-2008 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.2
 * @version    CVS: $Id: Report.php
 * @link       http://pms.mezimedia.com/
 * @deprecated File deprecated in Release 1.0
 */

class Task extends Zend_Db_Table
{	
	public $_db = null;
	public $_filter_list =array();
	public $_raw_task_list = array();
	public $_sup_task_list = array();
	
	public $_entities = array(
		'property'	=> array('-- Select --', 'SMUS', 'TSK', 'SHPCUS', 'SHPCEU', 'CTP', 'CMUS', 'CMs', 'MR'),
		'status'	=> array('-- Select --', 'Wait', 'WIP', 'Test', 'Cancel', 'Pause', 'Done'),
		'prior'		=> array('-- Select --', 'Urgent', 'High', 'Medium', 'Low'),
		'requester'	=> array('-- Select --', 'LA colleague', 'SHO colleague'),
		'manager'	=> array('-- Select --', 'William', 'Zhang Yong', 'Bonbon'),
		'type'		=> array('-- Select --', 'Type 1', 'Type 2', 'Type 3'),
		'resource'	=> array('-- Select --', 'User 1', 'User 2', 'User 3'),
		'delay'		=> array('-- Select --', 'Yes', 'No')
	);
	
	public function __construct () {
		$this->_db = Zend_Registry :: get('db_adapter');
		$this->_name = 'tt_st_task';
		$this->_primary = 'id';
		parent::_setup();
	}
	
	public function setFilter ($filter_list = array()) {
		$this->_filter_list = $filter_list;
	}
	
	public function getFilter ($prefix = '') {
		if (sizeof($this->_filter_list) <= 0) return '';
		$filter_arr = array();
		foreach ($this->_filter_list as $k => $v) {
			if ($v <= 0) continue;
			array_push($filter_arr, $k.' = '.$v);
		}
		if (sizeof($filter_arr) > 0) {
			return $prefix.implode(' and ', $filter_arr);
		} else {
			return '';
		}
	}
	
	public function update (array $data, $id) {
		$where = $this->_db->quoteInto('id = ?', $id);
		return parent::update($data, $where);
	}
	
	public function getCols () {
		return $this->_cols;
	}
	
	public function getTotal () {
		$sql  = 'select count(*) from '.$this->_name.' where sup_task_id = 0';
		$sql .= $this->getFilter(' and ');
		return $this->_db->fetchOne($sql);
	}
	
	public function getEntities ($type = null) {
		if ($type) {
			return $this->_entities[$type];
		}
		return $this->_entities;
	}

	public function getEntityValue ($type = null, $key = null) {
		if ($type) {
			$entity = $this->_entities[$type];
			if ($key) {
				return $entity[$key];
			}
		}
		return '--';
	}
	
	public function getRawTaskList ($sup_task_ids = array(0)) {
		try{
			$where = $this->_db->quoteInto('sup_task_id IN (?)', $sup_task_ids);
			$sup_tasks = $this->fetchAll($where, 'sup_task_id asc, id');
			if (count($sup_tasks)) {
				$sup_task_ids = array(); // reset sup_task_ids
				foreach ($sup_tasks as $sup_task) {
					array_push($this->_raw_task_list, $sup_task->toArray());
					array_push($sup_task_ids, $sup_task->__get('id'));
				}
//				Utils::dumper($sup_task_ids);
				// call self to rebuild again
				if (sizeof($sup_task_ids) > 0) {
					$this->getRawTaskList($sup_task_ids);
				} 
			}
		} catch (Zend_Db_Table_Exception $e) {
			echo $e->getMessage();
		}
	}
	
	public function getSuperTaskIds ($count = 1, $offset = 0) {
		$sup_task_ids = array(-1);
		try{
			$where  = $this->_db->quoteInto('sup_task_id = ?', 0);
			$where .= $this->getFilter(' and ');
//			echo $where;
			$sup_tasks = $this->fetchAll($where, 'id', $count, $offset);
			if (count($sup_tasks)) {
				foreach ($sup_tasks as $sup_task) {
					array_push($this->_raw_task_list, $sup_task->toArray());
					array_push($sup_task_ids, $sup_task->__get('id'));
				}
			}
		} catch (Zend_Db_Table_Exception $e) {
			echo $e->getMessage();
		}
		return $sup_task_ids;
	}

}
?>